package com.portal.cwegportal.service;

import com.appcenter.jz.app.CurrentUser;
import com.appcenter.jz.unit.ObjectUnit;
import com.appcenter.jz.page.PagedResult;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.portal.cwegportal.input.privateconfigure.*;
import com.portal.cwegportal.mapper.PrivateConfigureMapper;
import com.portal.cwegportal.model.PrivateConfigure;
import com.portal.cwegportal.output.privateconfigure.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.util.Date;
import java.util.List;
/**
 *  创建人:王增立
 *  备注: 个人中心配置服务
 *  时间: 2019/4/26 15:18
 *  参数: 
 */
@Service
public class PrivateConfigureService {
    @Autowired
    PrivateConfigureMapper privateConfigureMapper;
    /**
     *  创建人:王增立
     *  备注: 创建个人中心信息
     *  时间: 2019/4/26 15:18
     *  参数: 
     */
    @Transactional
    public CreatePrivateConfigureOutput createPrivateConfigure(CreatePrivateConfigureInput input) throws Exception {
        CreatePrivateConfigureOutput output = new CreatePrivateConfigureOutput();
        PrivateConfigure PrivateConfigure=new PrivateConfigure();
        ObjectUnit.Copy(input.getItem(),PrivateConfigure);
        PrivateConfigure.setGmtCreate(new Date());
        PrivateConfigure.setDeleted(false);
        PrivateConfigure.setCreatorCode(CurrentUser.getUserCode());
        PrivateConfigure.setCreatorId(CurrentUser.getUserId());
        PrivateConfigure.setCreatorName(CurrentUser.getUserName());
        PrivateConfigure.setUserId(CurrentUser.getUserId());
        privateConfigureMapper.insertSelective(PrivateConfigure);
        output.setItem(PrivateConfigure);
        return output;
    }
/**
 *  创建人:王增立
 *  备注: 保存，修改个人配置
 *  时间: 2019/4/26 15:18
 *  参数: 
 */
    @Transactional
    public EditPrivateConfigureOutput savePrivateConfigure (EditPrivateConfigureInput input) throws Exception{
        EditPrivateConfigureOutput output=new EditPrivateConfigureOutput();
        PrivateConfigure pc=this.getCurrentUserPrivateConfigure().getItem();
        PrivateConfigure outputPc=null;
        if(pc!=null){
            input.getItem().setId(pc.getId());//根据id 更新老配置的信息
            outputPc=this.editPrivateConfigure(input).getItem();

        }
        else {
            CreatePrivateConfigureInput cInput= new CreatePrivateConfigureInput();
            cInput.setItem(input.getItem());
            outputPc=this.createPrivateConfigure(cInput).getItem();
        }
        output.setItem(outputPc);
        return  output;
    }
/**
 *  创建人:王增立
 *  备注: 编辑个人配置
 *  时间: 2019/4/26 15:19
 *  参数: 
 */
    @Transactional
    public EditPrivateConfigureOutput editPrivateConfigure (EditPrivateConfigureInput input) throws Exception{
        EditPrivateConfigureOutput output=new EditPrivateConfigureOutput();
        PrivateConfigure oldEntity= privateConfigureMapper.selectByPrimaryKey(input.getItem().getId());
        //oldEntity.setDefaultConfig(input.getItem().getDefaultConfig());
        oldEntity.setCustomConfig(input.getItem().getCustomConfig());
        oldEntity.setSkinConfig(input.getItem().getSkinConfig());
        oldEntity.setGmtModified(new Date());
        oldEntity.setUserId(CurrentUser.getUserId());
        privateConfigureMapper.updateByPrimaryKey(oldEntity);
        output.setItem(oldEntity);
        return  output;
    }


/**
 *  创建人:王增立
 *  备注: 清除个人配置
 *  时间: 2019/4/26 15:20
 *  参数: 
 */
    @Transactional
    public DeletePrivateConfigureOutput deletePrivateConfigure(DeletePrivateConfigureInput input) {
        DeletePrivateConfigureOutput output = new DeletePrivateConfigureOutput();
        PrivateConfigure oldEntity = privateConfigureMapper.selectByPrimaryKey(input.getItem().getId());
        oldEntity.setDeleted(true);
        privateConfigureMapper.updateByPrimaryKey(oldEntity);
        return output;
    }
/**
 *  创建人:王增立
 *  备注: 获取个人配置
 *  时间: 2019/4/26 15:20
 *  参数: 
 */
    @Transactional
    public GetPrivateConfiguresOutput getPrivateConfigures(GetPrivateConfiguresInput input) {
        GetPrivateConfiguresOutput output = new GetPrivateConfiguresOutput();

        //logger.error("log4j2 error!");

        PageHelper.startPage(input.getPage().getPageNo(),input.getPage().getPageSize(),"Id desc");
        //List<Menus> ls = MenuMapper.SearchMenus(input);
        Example example = new Example(PrivateConfigure.class);
        Example.Criteria criteria=example.createCriteria();
        criteria.andEqualTo("deleted",0);
        List<PrivateConfigure> ls=privateConfigureMapper.selectByExample(example);
        PageInfo<PrivateConfigure> page = new PageInfo<>(ls);
        output.setItems(new PagedResult<PrivateConfigure>(page.getList(),(int)page.getTotal()));
        return  output;

    }
/**
 *  创建人:王增立
 *  备注: 根据当前人员信息获取个人中心配置
 *  时间: 2019/4/26 15:20
 *  参数: 
 */
    public GetPrivateConfigureOutput getCurrentUserPrivateConfigure() {
        GetPrivateConfigureOutput output = new GetPrivateConfigureOutput();
        //List<Menus> ls = MenuMapper.SearchMenus(input);
        Example example = new Example(PrivateConfigure.class);
        Example.Criteria criteria=example.createCriteria();
        criteria.andEqualTo("deleted",0);
        criteria.andEqualTo("userId",CurrentUser.getUserId());
        PrivateConfigure model=privateConfigureMapper.selectOneByExample(example);
        output.setItem(model);
        return  output;
    }
    /**
     *  创建人:王增立
     *  备注: 加载的时候，获取个人中心配置
     *  时间: 2019/4/26 18:08
     *  参数: 
     */
    public PageLoadOutput pageLoad(PageLoadInput input) {
        PageLoadOutput output = new PageLoadOutput();
        output.setItems(getPrivateConfigures(input).getItems());
        return output;
    }
}
